package com.zjt.myjiakao;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import com.zjt.myjiakao.adapter.TopicAdapter;
import com.zjt.myjiakao.fragment.ReadFragment;
import com.zjt.myjiakao.pojo.KeMuYiPoJo;
import com.zjt.myjiakao.view.ReaderViewPager;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static java.util.Collections.shuffle;

public class RandActivity extends AppCompatActivity {

    private SlidingUpPanelLayout mLayout;
    private TopicAdapter topicAdapter;
    private RecyclerView recyclerView;
    private ImageView shadowView;
    private ReaderViewPager readerViewPager;
    private List<KeMuYiPoJo.ResultBean> datas;
    private TextView bt_load_anwer;
    private ProgressBar progressBar;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            progressBar.setVisibility(View.GONE);
            shuffle(datas);
            Log.e("data.size=", "" + datas.size());

            if (topicAdapter != null) {
                topicAdapter.setDataNum(datas.size());
            }
            initReadViewPager();
            bt_load_anwer.setText(1 + "/" + datas.size());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rand);
        ImageView bt_pre = findViewById(R.id.bt_pre);
        ImageView bt_next = findViewById(R.id.bt_next);
        bt_load_anwer = findViewById(R.id.bt_load_anwer);
        progressBar = findViewById(R.id.progressBar);
        progressBar.setVisibility(View.VISIBLE);
        initSlidingUoPanel();
        initList();

        new Thread(new Runnable() {
            @Override
            public void run() {
                KeMuYiPoJo anwerInfo = getAnwer();
                datas = anwerInfo.getResult();
                handler.sendEmptyMessage(1);
            }
        }).start();

        bt_pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentItem = readerViewPager.getCurrentItem();
                if (currentItem == 0) {
                    Toast.makeText(getBaseContext(), "已经是第一题了！", Toast.LENGTH_SHORT).show();
                }
                currentItem = currentItem - 1;
                if (currentItem > datas.size() - 1) {
                    currentItem = datas.size() - 1;
                }
                readerViewPager.setCurrentItem(currentItem, true);
            }
        });

        bt_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentItem = readerViewPager.getCurrentItem();
                if (currentItem == datas.size()-1) {
                    Toast.makeText(getBaseContext(), "已经是最后一题了！", Toast.LENGTH_SHORT).show();
                }
                currentItem = currentItem + 1;
                if (currentItem < 0) {
                    currentItem = 0;
                }
                readerViewPager.setCurrentItem(currentItem, true);
            }
        });
    }

    private int prePosition2;
    private int curPosition2;

    private void initReadViewPager() {
        shadowView = (ImageView) findViewById(R.id.shadowView);
        readerViewPager = (ReaderViewPager) findViewById(R.id.readerViewPager);
        readerViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                KeMuYiPoJo.ResultBean subDataBean = datas.get(position);
                return ReadFragment.newInstance(subDataBean, position);
            }

            @Override
            public int getCount() {
                return datas.size();
            }
        });
        readerViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                shadowView.setTranslationX(readerViewPager.getWidth() - positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                curPosition2 = position;
                bt_load_anwer.setText(position + 1 + "/" + datas.size());
                topicAdapter.notifyCurPosition(curPosition2);
                topicAdapter.notifyPrePosition(prePosition2);
                prePosition2 = curPosition2;

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private int prePosition;
    private int curPosition;

    private void initList() {
        recyclerView = (RecyclerView) findViewById(R.id.list);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6);

        topicAdapter = new TopicAdapter(this);

        recyclerView.setLayoutManager(gridLayoutManager);

        recyclerView.setAdapter(topicAdapter);


        topicAdapter.setOnTopicClickListener(new TopicAdapter.OnTopicClickListener() {
            @Override
            public void onClick(TopicAdapter.TopicViewHolder holder, int position) {
                curPosition = position;
                Log.i("点击了==>", position + "");
                if (mLayout != null &&
                        (mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED || mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED)) {
                    mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
                }
                readerViewPager.setCurrentItem(position);
                topicAdapter.notifyCurPosition(curPosition);
                topicAdapter.notifyPrePosition(prePosition);
                prePosition = curPosition;
            }
        });


    }

    private void initSlidingUoPanel() {
        mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);

        int height = getWindowManager().getDefaultDisplay().getHeight();

        LinearLayout dragView = (LinearLayout) findViewById(R.id.dragView);
        SlidingUpPanelLayout.LayoutParams params = new SlidingUpPanelLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (height * 0.8f));
        dragView.setLayoutParams(params);


        mLayout.addPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {
                Log.i("", "onPanelSlide, offset " + slideOffset);
            }
            @Override
            public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) {
                Log.i("", "onPanelStateChanged " + newState);
            }
        });
        mLayout.setFadeOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
            }
        });
    }


    @Override
    public void onBackPressed() {
        if (mLayout != null &&
                (mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED || mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED)) {
            mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
        } else {
            super.onBackPressed();
        }
    }

    public String inputStream2String(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = -1;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        return baos.toString();
    }

    private KeMuYiPoJo getAnwer() {
        try {
            InputStream in = getAssets().open("kemu1.json");
            KeMuYiPoJo anwerInfo = JSON.parseObject(inputStream2String(in), KeMuYiPoJo.class);
            return anwerInfo;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("data.size=", e.toString());
        }
        return null;
    }

}
